﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace quanlyktx.quanlysinhvien
{
    public partial class frmsinhvien : DevComponents.DotNetBar.Office2007Form 
    {
        public frmsinhvien()
        {
            InitializeComponent();
        }
        ketnoi kn = new ketnoi();
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();
        private void btthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void groupPanel2_Click(object sender, EventArgs e)
        {

        }
        private void laylop()
        {
            SqlConnection cn = kn.taokn();
            cn.Open();
            SqlCommand cmd = new SqlCommand("tb_lop_SelectAll", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            da.SelectCommand = cmd;
            da.Fill(dt);
            cblop.DataSource = dt;
            cblop.DisplayMember = "tenlop";
            cblop.ValueMember = "malop";
        }
        private void layphong()
        {
            SqlConnection cn = kn.taokn();
            cn.Open();
            SqlCommand cmd = new SqlCommand("tb_phong_SelectAll", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            da.SelectCommand = cmd;
            da.Fill(dt);
            cblop.DataSource = dt;
            cblop.DisplayMember = "tenphong";
            cblop.ValueMember = "maphong";
        }
        private void btthem_Click(object sender, EventArgs e)
        {
            if (txttensv.Text == "" || txtcmnd.Text == "" || txtdiachi.Text == "" || txtnhanvien.Text == "")
            {
                MessageBox.Show("dữ liệu ko dược để trống", "lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                SqlConnection cn = kn.taokn();
                cn.Open();
                SqlCommand cmd = new SqlCommand("tb_sinhvien_Insert", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@tensinhvien", SqlDbType.NVarChar).Value = txttensv.Text;
                cmd.Parameters.Add("@ngaysinh", SqlDbType.DateTime).Value = dtngaysinh.Value;
                bool  gt;
                if (rbnam.Checked == true)
                {
                    gt = true ;
                }
                else gt =false ;
                cmd.Parameters.Add("@gioitinh", SqlDbType.NVarChar).Value = gt;
                cmd.Parameters.Add("@socmnd", SqlDbType.Int).Value = txtcmnd.Text;
                cmd.Parameters.Add("@diachi", SqlDbType.NVarChar).Value = txtdiachi.Text;
                cmd.Parameters.Add("@sdt", SqlDbType.Int).Value = txtsdt.Text;
               // cmd.Parameters.Add("@makhoa", SqlDbType.Int).Value = cbkhoa.Text;
                cmd.Parameters.Add("@malop", SqlDbType.Int).Value = cblop.Text;
                cmd.Parameters.Add("@manhanvien", SqlDbType.Int).Value = txtnhanvien.Text;
                cmd.Parameters.Add("@ngayvao", SqlDbType.DateTime).Value = dtngayvao.Value;
                cmd.Parameters.Add("@ngayra", SqlDbType.DateTime).Value = dtngayra.Value;
                cmd.Parameters.Add("@anh", SqlDbType.NVarChar).Value = anhsinhvien;
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                MessageBox.Show("bạn đã thêm thành công", "thông báo");
            }
        }

        private void btsua_Click(object sender, EventArgs e)
        {
            if (txttensv.Text == "" || txtcmnd.Text == "" || txtdiachi.Text == "" || txtnhanvien.Text == "")
            {
                MessageBox.Show("dữ liệu ko dược để trống", "lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                SqlConnection cn = kn.taokn();
                cn.Open();
                SqlCommand cmd = new SqlCommand("tb_sinhvien_Update", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@masinhvien", SqlDbType.Int).Value = txtmasv.Text;
                cmd.Parameters.Add("@tensinhvien", SqlDbType.NVarChar).Value = txttensv.Text;
                cmd.Parameters.Add("@ngaysinh", SqlDbType.DateTime).Value = dtngaysinh.Value;
                bool  gt;
                if (rbnam.Checked == true)
                {
                    gt = true ;
                }
                else gt =false ;
                cmd.Parameters.Add("@gioitinh", SqlDbType.NVarChar).Value = gt;
                cmd.Parameters.Add("@socmnd", SqlDbType.Int).Value = txtcmnd.Text;
                cmd.Parameters.Add("@diachi", SqlDbType.NVarChar).Value = txtdiachi.Text;
                cmd.Parameters.Add("@sdt", SqlDbType.Int).Value = txtsdt.Text;
               // cmd.Parameters.Add("@makhoa", SqlDbType.Int).Value = cbkhoa.Text;
                cmd.Parameters.Add("@malop", SqlDbType.Int).Value = cblop.Text;
                cmd.Parameters.Add("@manhanvien", SqlDbType.Int).Value = txtnhanvien.Text;
                cmd.Parameters.Add("@ngayvao", SqlDbType.DateTime).Value = dtngayvao.Value;
                cmd.Parameters.Add("@ngayra", SqlDbType.DateTime).Value = dtngayra.Value;
                cmd.Parameters.Add("@anh", SqlDbType.NVarChar).Value = anhsinhvien;
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                MessageBox.Show("bạn đã thêm thành công", "thông báo");
            }
        }

        private void btxoa_Click(object sender, EventArgs e)
        {
            SqlConnection cn = kn.taokn();
            cn.Open();
            SqlCommand cmd = new SqlCommand("tb_sinhvien_Delete", cn);
            if (MessageBox.Show("bạn có thực sự muốn xóa sinh viên:" + txtmasv .Text + "này ", "thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@masinhvien", SqlDbType.Int).Value = txtmasv .Text;
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                MessageBox.Show("bạn đã xóa thành công");

            }
        }

        private void bttimkiem_Click(object sender, EventArgs e)
        {
            SqlConnection cn = kn.taokn();
            cn.Open();
            SqlCommand cmd = new SqlCommand("tb_sinhvien_GetByID", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@masinhvien", SqlDbType.NVarChar).Value = txttimkiem.Text;
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            cn.Close();
        }

        private void frmsinhvien_Load(object sender, EventArgs e)
        {
            string sql = "tb_sinhvien_SelectAll";
            dgsinhvien.DataSource = kn.taobang(sql);
            txtmasv.DataBindings.Clear();
            txtmasv.DataBindings.Add("Text", dgsinhvien.DataSource, "masinhvien");
            txttensv.DataBindings.Clear();
            txttensv.DataBindings.Add("Text", dgsinhvien.DataSource, "tensinhvien");
            dtngaysinh.DataBindings.Clear();
            dtngaysinh.DataBindings.Add("Text", dgsinhvien.DataSource, "ngaysinh");
            txtcmnd.DataBindings.Clear();
            txtcmnd.DataBindings.Add("Text", dgsinhvien.DataSource, "socmnd");
            txtsdt.DataBindings.Clear();
            txtsdt.DataBindings.Add("Text", dgsinhvien.DataSource, "sdt");
            txtnhanvien.DataBindings.Clear();
            txtnhanvien.DataBindings.Add("Text", dgsinhvien.DataSource, "manhanvien");
            txtdiachi.DataBindings.Clear();
            txtdiachi.DataBindings.Add("Text", dgsinhvien.DataSource, "diachi");
            cblop.DataBindings.Clear();
            cblop.DataBindings.Add("Text", dgsinhvien.DataSource, "malop");
            cbphong.DataBindings.Clear();
            cbphong.DataBindings.Add("Text", dgsinhvien.DataSource, "maphong");
            laylop();
            layphong();

        }
        string anhsinhvien;
        private void llabanh_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            OpenFileDialog dl = new OpenFileDialog();
            dl.InitialDirectory = Application.StartupPath + @"anhsinhvien/";
            if (dl.ShowDialog() == DialogResult.OK)
            {
                anhsinhvien  = dl.FileName.Substring(dl.FileName.LastIndexOf("\\") + 1, dl.FileName.Length - dl.FileName.LastIndexOf("\\") - 1);
                ptanh.Image = new Bitmap(Application.StartupPath + @"\anhsinhvien\" + anhsinhvien );
                ptanh .SizeMode = PictureBoxSizeMode.StretchImage;
            }
        }
    }
}
